import 'package:flutter/material.dart';
import 'package:flutter_trip/model/common_model.dart';
import 'package:flutter_trip/model/sales_box_model.dart';

import 'webview.dart';

class SalesBox extends StatelessWidget {
  final SalesBoxModel? salesBox;

  const SalesBox({super.key, required this.salesBox});

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: _items(context),
    );
  }

  _items(BuildContext context) {
    if (salesBox == null) return null;
    List<Widget> items = [];
    items.add(_doubleItem(
        context, salesBox!.bigCard1!, salesBox!.bigCard2!, true, false));
    items.add(_doubleItem(
        context, salesBox!.smallCard1!, salesBox!.smallCard2!, false, false));
    items.add(_doubleItem(
        context, salesBox!.smallCard3!, salesBox!.smallCard4!, false, false));
    return Column(
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        Container(
          height: 44,
          margin: const EdgeInsets.only(left: 10),
          decoration: const BoxDecoration(
            border:
                Border(bottom: BorderSide(width: 1, color: Color(0xFFf2f2f2))),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Image.network(
                salesBox!.icon!,
                height: 16,
                fit: BoxFit.contain,
              ),
              Container(
                padding: const EdgeInsets.fromLTRB(10, 1, 8, 1),
                margin: const EdgeInsets.only(right: 8),
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(12),
                    gradient: const LinearGradient(
                        colors: [Color(0xFFff4e63), Color(0xFFff6cc9)],
                        begin: Alignment.centerLeft,
                        end: Alignment.centerRight)),
                child: GestureDetector(
                  onTap: () {
                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) {
                      return WebView(
                        url: salesBox!.moreUrl!,
                        title: '更多活动',
                      );
                    }));
                  },
                  child: const Text(
                    "获取更多福利 >",
                    style: TextStyle(color: Colors.white, fontSize: 12),
                  ),
                ),
              ),
            ],
          ),
        ),
        ...items,
      ],
    );
  }

  _doubleItem(BuildContext context, CommonModel leftCard, CommonModel rightCard,
      bool big, bool last) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceAround,
      children: [
        _item(context, leftCard, big, true, last),
        _item(context, rightCard, big, false, last),
      ],
    );
  }

  _item(
      BuildContext context, CommonModel card, bool big, bool left, bool last) {
    BorderSide borderSide =
        const BorderSide(width: 0.8, color: Color(0xFFf2f2f2));
    return GestureDetector(
      onTap: () {
        Navigator.push(context,
            MaterialPageRoute(builder: (context) {
              return WebView(
                url: card.url!,
                statusBarColor: card.statusBarColor,
                hideAppBar: card.hideAppBar,
              );
            }));
      },
      child: Container(
        decoration: BoxDecoration(
          border: Border(
            right: left ? borderSide : BorderSide.none,
            bottom: last ? BorderSide.none : borderSide,
          ),
        ),
        child: Image.network(
          card.icon!,
          width: MediaQuery.of(context).size.width / 2 - 14,
          height: big ? 130 : 80,
        ),
      ),
    );
  }
}
